/***
This do file combines the worksheets produced earlier in the pipeline to update tables.xlsx
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
include "${root}/code/set_globals.do"

* Programs
cap program drop assign_excel_sheet
program define assign_excel_sheet

	/*** Takes an Excel sheet, and copies it into a different Excel file, overwriting existing sheets if applicable
	***/
	
	syntax using/, to(string) sheet(name)
	
	project, uses(`"`using'"')
	import excel using `"`using'"', sheet(`sheet') clear
	
	export excel using `"`to'"', sheet(`sheet', replace)

end

*-------------------------------------------------------------------------------
* Tables
*-------------------------------------------------------------------------------

* This is an excel sheet that reads raw data sheets and outputs formatted tables 
project, uses("${root}/paper/tables_formatting.xlsx")

* tables.xlsx is an excel sheet that reads UPDATED raw data sheets and outputs formatted tables
copy "${root}/paper/tables_formatting.xlsx" "${root}/results/tables.xlsx", replace	

assign_excel_sheet using "${root}/results/new_table_1.xlsx", sheet(new_table_1) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_table_2.xlsx", sheet(new_table_2) to("${root}/results/tables.xlsx")

assign_excel_sheet using "${root}/results/new_app_table_3.xlsx", sheet(new_app_table_3) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_5_a.xlsx", sheet(new_app_table_5_a) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_5_b_c.xlsx", sheet(new_app_table_5_b_c) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_6.xlsx", sheet(new_app_table_6) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_7.xlsx", sheet(new_app_table_7) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_8_a_b.xlsx", sheet(new_app_table_8_a_b) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_8_c.xlsx", sheet(new_app_table_8_c) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_10.xlsx", sheet(new_app_table_10) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_11.xlsx", sheet(new_app_table_11) to("${root}/results/tables.xlsx")
assign_excel_sheet using "${root}/results/new_app_table_12.xlsx", sheet(new_app_table_12) to("${root}/results/tables.xlsx")

project, creates("${root}/results/tables.xlsx")
